#ifndef _THREAD_H_
#define _THREAD_H_
#include <iostream>
#include <string>
#include <pthread.h>
#include <functional>
#include <cstdio>
#include <cstring>
using namespace std;
int number = 0;
namespace mypthread
{
    template <typename T>
    class thread
    {
    private:
        using functions_type = function<void(T)>;
    public:
        thread(functions_type func, T data)
            : _tid(0), _isdetach(false), _isrunning(false), _data(data), _func(func), _res(nullptr)
        {
            _name = "thread-" + to_string(number++) +" ";
        }
        void enablerunning()
        {
            _isrunning = true;
        }
        static void* routine(void *args)
        {
            thread<T> *self = static_cast<thread<T> *>(args);
            self->_func(self->_data);
            self->enablerunning();
            return nullptr;
        }
        // 分离线程
        void detach()
        {
            if (!_isrunning)
            {
                cout << "thread has not been started" << endl;
                return;
            }

            int n = pthread_detach(_tid);
            if (n != 0)
            {
                cout << "detach thread failed:" << strerror(n) << endl;
                return;
            }
            else
            {
                _isdetach = true;
                cout << _name << "detach thread success" << endl;
                return;
            }
        }
        // 等待线程结束
        void join()
        {
            // 如果已经分离，则不需要等待
            if (_isdetach)
            {
                cout << "thread has been detach" << endl;
                return;
            }
            pthread_join(_tid, &_res);
            cout<<_name<<"join thread success"<<endl;
            return;
        }
        // 消除一个进程
        void cancel()
        {
            int n = pthread_cancel(_tid);
            if (n != 0)
            {
                cout << "cancel thread failed:" << strerror(n) << endl;
                return;
            }
            else
            {
                _isrunning = false;
                cout << _name << "cancel thread success" << endl;
                return;
            }
        }
        // 获取线程ID
        pthread_t get_tid()
        {
            return _tid;
        }
        // 开始创建一个进程
        void start()
        {
            int n = pthread_create(&_tid, nullptr, routine, this);
            if (n != 0)
            {
                cout << "create thread failed:" << strerror(n) << endl;
                return;
            }
            else
            {
                cout << "create thread success" << endl;
            }
            return;
        }
        ~thread()
        {
        }

    private:
        // 线程的名字
        string _name;
        // 线程的ID
        pthread_t _tid;
        // 线程的状态
        bool _isdetach;  // 是否被分离
        bool _isrunning; /// 是否正在运行
        // 线程数据
        T _data;
        // 线程函数
        functions_type _func;
        // 线程返回值
        void *_res;
    };
}
#endif